Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3490 removing Content-Type and Transfer-Encoding headers on redirect #3493

Merged
merged 2 commits into from
Aug 12, 2016

Conversation

nateprewitt
Copy link
Member

This will remove additional headers (Content-Type and Transfer-Encoding) from non-temporary/non-permanent redirects in resolve_redirects to address #3490.

This functionality isn't currently being tested but I'm happy to add a test or two for it if desired.

@Lukasa
Copy link
Member

Lukasa commented Aug 10, 2016

I would like to see tests for this please! ✨

@saveman71
Copy link

I was on it too. Did not find a way to properly add tests though since httpbin does not seem to be able to post to an endpoint and respond with a 301/302. Am I wrong on this one?

Anyway since @nateprewitt is on it I will not open my PR ;)

@saveman71
Copy link

On my version of the fix (kennethreitz/requests@master...saveman71:master) I moved this line a little higher and removed the try/catch below as it seems the general way (at least in this file) is to check the existence of the key prior to the deletion.

@nateprewitt
Copy link
Member Author

@saveman71, sorry, I didn't mean to jump on your PR. I wasn't sure if anyone had started working on it.

@Lukasa I added a test to check the header removal and empty body.

@nateprewitt
Copy link
Member Author

@Lukasa re: this line

Do you have an opinion on moving the declaration farther up to avoid the multiple calls to the headers attribute on the prepare_request? Also as for the conversion from try/except, if that was done would you want it in a separate PR to avoid semi-unrelated changes?

@saveman71
Copy link

saveman71 commented Aug 10, 2016

@nateprewitt no worries at all, I should have stated I started working on it.
As for the line thing, I do not think it is a performance issue (I believe, but I might be mistaken) because well requests is a network library and "premature optimization is the root of all evil". (https://www.xkcd.com/1691/)

I was suggesting that merely because it's already been like that here and here for example.

On a side note, 👍 for a separate PR.

@nateprewitt
Copy link
Member Author

nateprewitt commented Aug 10, 2016

@saveman71, I only bring up the optimization because @sigmavirus24 has brought it up before. If I'm misunderstanding his original intent though, I vote we completely remove the declaration, in favor of prepared_request.headers. headers currently doesn't provide a lot of use in resolve_redirects.

Edit: Clarity and Link

@nateprewitt nateprewitt force-pushed the 3490_drop_headers branch 3 times, most recently from 0ef19b9 to 394b353 Compare August 10, 2016 19:17
@saveman71
Copy link

Quick semi-unrelated question, what would be the timeline of a release on PyPI after this gets merged?

cc @Lukasa

@Lukasa
Copy link
Member

Lukasa commented Aug 10, 2016

We'd be aiming to release this early next week if we can merge it this week.

@Lukasa
Copy link
Member

Lukasa commented Aug 10, 2016

To be clear, this is on my queue, but I'll review tomorrow.

# https://github.com/kennethreitz/requests/issues/3490
purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding')
for header in purged_headers:
if header in prepared_request.headers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I wrote this poorly the first time, but why don't we do:

prepared_request.headers.pop('Content-Length', None)
prepared_request.headers.pop('Content-Type', None)

Or if you really want to shave the yak

for part in ('Length', 'Type'):
    prepared_request.headers.pop('Content-' + part, None)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, I think the second suggestion is very bad, but I realize some people might actually prefer that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally used pop over del when writing it, but reverted to keep with the surrounding code. If you're onboard with using pop instead, let's do it.

@@ -221,6 +221,23 @@ def test_http_303_doesnt_change_head_to_get(self, httpbin):
assert r.history[0].status_code == 303
assert r.history[0].is_redirect

def test_header_and_body_removal_on_redirect(self, httpbin):
purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't actually cover 'Transfer-Encoding' since 'Content-Length' is present. I can add a smaller separate test for 'Transfer-Encoding' if it's deemed that we definitely want to remove it.

This does ensure that as long as purged_headers contains 'Transfer-Encoding' in resolve_redirects, that it will be removed if it occurs. It just won't fail for accidental removal from the purged_headers tuple.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transfer-Encoding should be easily covered: a generator body will trigger it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transfer-Encoding test code added.

assert next_resp_tran.request.body is None
for header in purged_headers:
assert header not in next_resp.request.headers
assert header not in next_resp_tran.request.headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split this into two tests that look very similar, rather than have one really complex test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. Tests split up now.

@Lukasa
Copy link
Member

Lukasa commented Aug 11, 2016

Alright, fab, I'm happy with this. @sigmavirus24 you ok with us merging this for 2.11.1?

req = requests.Request('POST', httpbin('post'), data=(b'x' for x in range(1)))
prep = ses.prepare_request(req)
assert 'Transfer-Encoding' in prep.headers
resp = ses.send(prep)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reduced the range call to size 1 because we were hitting this bug when sending more than one chunk. After some more testing, this only passes most of the time, which likely isn't something we want to introduce.

The failures seem random and are tied specifically to the httpbin module. Calling the live http://httpbin.org/post endpoint won't produce the failure.

As I see it, two options are either calling the httpbin.org/post endpoint directly (probably not the best solution) or adding a try/except around resp = ses.send(prep) and manually creating the Response on failure.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a question about whether we should just manually create the response unconditionally, to be honest. That might be the most sensible thing to do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'll make a note and create the Response manually for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, test updated with manual Response. If it looks good I'll squash things down.

@Lukasa
Copy link
Member

Lukasa commented Aug 12, 2016

Alright, all is well. @sigmavirus24, you want this in 2.11.1?

@sigmavirus24
Copy link
Contributor

👍

@sigmavirus24 sigmavirus24 merged commit f5ff345 into psf:master Aug 12, 2016
@nateprewitt nateprewitt deleted the 3490_drop_headers branch August 12, 2016 22:02
@nateprewitt nateprewitt restored the 3490_drop_headers branch November 3, 2016 22:13
@nateprewitt nateprewitt deleted the 3490_drop_headers branch November 3, 2016 22:53
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants